\name{DetermineVisits}
\alias{DetermineVisits}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{ Determine Visit Number for survey }
\description{
  Determine a unique visit number for each survey performed at a
  site (station), in order to group surveys that might span more
  than 1 day
}
\usage{
DetermineVisits(data = NULL, same.visit = 7)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
  \item{data}{ a data.frame of the same form as the retrieved from \link{getPCdata}}
  \item{same.visit}{ The interval (in days) that denote a single survey for a given station. }
}
\details{
  There is an assumption of closure during point count surveys.
  However, many times, due to extenuating circumstances (weather,
  daylight, disturbance, etc.) a survey at a particular site may
  end up spanning into a second day.  This function groups any
  surveys at the same site(station) within a user-defined number
  of days (same.vist).  The default is that all surveys
  performed at the same site within 7 days are part of the same
  survey.
  
  ToDO:  Currently this will work to generate the right visits
  for datasets that do not have issues.  It still needs work 
  to tie appropriate visits together, if points within a station
  did get surveyed on different days.
}
\value{
A vector the same length as the number of rows 
}

\author{ Mark Herzog \email{mherzog@prbo.org} }
\examples{
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--    or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function(data=NULL, same.visit=7) {
    if (is.null(data)==TRUE) stop("A data file is required.")
    data$visit<-0
    for (bygrp in levels(factor(paste(data$SamplingUnitID,data$point,data$year,sep="")))) {
        visit<-1
        j<-nrow(data[paste(data$SamplingUnitID,data$point,data$year,sep="")==bygrp,])
        if (j==0 | j>same.visit) {
            for (VisitDate in levels(factor(data[paste(data$SamplingUnitID,data$point,data$year,sep="")==bygrp,]$date))) {
                data[paste(data$SamplingUnitID,data$point,data$year,sep="")==bygrp & data$date==as.Date(VisitDate),]$visit<-visit
                visit<-visit+1
            }
        }
    }
    return(data$visit)
  }
}
% Add one or more standard keywords, see file 'KEYWORDS' in the
% R documentation directory.
\keyword{ data }
\keyword{ manip }
